package com.ttt.game;

import com.ttt.tank.Tank;
import com.ttt.util.Constant;

import java.awt.*;

/**
 * 子弹类
 */
public class Bullet {
    //子弹默认的速度为坦克速度的2倍
    public static final int Default_Speed = Tank.Default_Speed << 1;
    //炮弹半径
    public static final int Radius = 4;
    private int x,y;
    private int speed = Default_Speed;
    private int dir;
    private int atk;
    private Color color;
    //子弹是否可见
    private boolean visible = true;

    public Bullet(int x, int y, int dir, int atk,Color color) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.atk = atk;
        this.color = color;
    }
    //给对象池使用的，所有的属性都是默认值。
    public Bullet() {
    }

    public void draw(Graphics g){
        if (!visible)return;
        logic();
        g.setColor(color);
        g.fillOval(x-Radius+2,y-Radius+1,Radius<<1,Radius<<1);
    }

    private void logic(){
        move();

    }

    private void move(){
        switch (dir){
            case Tank.Dir_Up:
                y -= speed;
                if (y <= 0)
                    visible = false;
                break;
            case Tank.Dir_Down:
                y += speed;
                if (y > Constant.Frame_Height){
                    visible = false;
                }
                break;
            case Tank.Dir_Left:
                x -= speed;
                if (x < 0){
                    visible = false;
                }
                break;
            case Tank.Dir_Right:
                x += speed;
                if (x > Constant.Frame_Width){
                    visible = false;
                }
                break;
        }
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public int getDir() {
        return dir;
    }

    public void setDir(int dir) {
        this.dir = dir;
    }

    public int getAtk() {
        return atk;
    }

    public void setAtk(int atk) {
        this.atk = atk;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public boolean isVisible() {
        return visible;
    }

    public void setVisible(boolean visible) {
        this.visible = visible;
    }
}
